<?php

/**
 * Helper for module urbac
 *
 * Creating Users table
 *
 * @author vlt
 * @package urbac.components
 * @since 1.0.1
 */

class UrbacHelper {

/**
 *
 * @param bool $drop Drop table if exists
 */
    public static function createTable() {


        $db = Yii::app()->db;
        $module = Yii::app()->getModule('urbac');
        $tableName = $module->usersTable;

        $transaction  = $db->beginTransaction();
        try {
            $db->createCommand("drop table if exists ".$tableName.";")->execute();
            switch($db->driverName){
                case 'sqlite':
                    $sql = "create table $tableName (
                                        id INTEGER  PRIMARY KEY  AUTOINCREMENT NOT NULL,
                                        username varchar(32) NOT NULL,
                                        password varchar(32) NOT NULL,
                                        email varchar(32) NOT NULL,
                                        valid_code varchar(32) DEFAULT NULL,
                                        active tinyint(1) NOT NULL DEFAULT 0
                                        );";
                    break;
                default:
                    $sql = "create table $tableName (
                                        id int(11) NOT NULL AUTO_INCREMENT,
                                        username varchar(32) NOT NULL,
                                        password varchar(32) NOT NULL,
                                        email varchar(32) NOT NULL,
                                        valid_code varchar(32) DEFAULT NULL,
                                        active tinyint(1) NOT NULL DEFAULT 0,
                                        primary key (id)
                                        );";
                    break;

            }

            $db->createCommand($sql)->execute();

            //creating admin user
            
            $username = (self::findModule('srbac')?self::findModule('srbac')->superUser:self::findModule('urbac')->superUser);

            $sql = "INSERT INTO ".$tableName." (username, password, email, active) VALUES ('".$username."','".self::hashWord('admin',false)."','".Yii::app()->params['adminEmail']."',1)";
            $db->createCommand($sql)->execute();
            $sql = "INSERT INTO ".$tableName." (username, password, email, active) VALUES ('user','".self::hashWord('user',false)."','".Yii::app()->params['adminEmail']."',1)";
            $db->createCommand($sql)->execute();

            $transaction->commit();
        }
        catch(CDbException $ex)  {
//            throw new CHttpException(666, $ex);
            $transaction->rollback();
        }
    }

    /**
     * Hash gives string
     *
     * @param string $word String to hash
     * @param bool $hash If use additional hash string
     * @return string Hashed word
     */
    public static function hashWord($word='', $hash=false) {
        if(empty($word)) return false;
        if($hash)
            $word .= Yii::app()->getModule('urbac')->hashWord;
        return md5($word);
    }




    /**
     *
     * Find a module searching in application modules and if it's not found there
     * looks in modules' modules
     * @param String $moduleID The model to find
     * @return The module, if it's found else null
     *
     * @package srbac.components
     */
    public static function findModule($moduleID) {
        if(Yii::app()->getModule($moduleID)) {
            return Yii::app()->getModule($moduleID);
        }
        $modules = Yii::app()->getModules();
        foreach ($modules as $mod=>$conf) {
            if(Yii::app()->getModule($mod)) {
                return self::findInModule(Yii::app()->getModule($mod), $moduleID);
            }
        }
        return null;
    }

    private static function findInModule($parent, $moduleID) {
        if ($parent->getModule($moduleID)) {
          return $parent->getModule($moduleID);
        } else {
          $modules = $parent->getModules();
          foreach ($modules as $mod => $conf) {
            return $this->findInModule($parent->getModule($mod), $moduleID);
          }
        }
        return null;
    }

}

?>
